<template>
  <view class="cart-container" v-if="carts.length != 0">
    <!-- 收货地址模块 -->
    <my-address></my-address>
    <!-- 购物车图标 -->
    <view class="cart-logo">
      <uni-icons type="cart" size="17"></uni-icons>
      <text>购物车</text>
    </view>
    <!-- 滑动删除 -->
    <uni-swipe-action>
      <block v-for="(good, i) in carts" :key="i">
        <!-- ios中需要使用right-options -->
        <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(good.goods_id)">
          <my-goodlist :radioShow="true" :numShow="true" :good="good" @click="goToGoodDetail(good.goods_id)"
            @changeStatus="handlerGoodsStatus" @changeNum="handlerGoodsNum()"></my-goodlist>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <!-- 结算 -->
    <my-settle></my-settle>
  </view>
<!-- 空白购物车区域 -->
  <view class="empty-cart" v-else>
    <image src="/static/cart_empty@2x.png" class="empty-img"></image>
    <text class="tip-text">空空如也~</text>
  </view>

</template>

<script>
  import tarbarBarge from '../../mixins/tarbar-barge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    mixins: [tarbarBarge],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000'
          }
        }]
      };
    },
    computed: {
      ...mapState('cart', ['carts'])
    },
    methods: {
      ...mapMutations('cart', ['updataGoodsStatus', 'updateGoodsNum', 'deleteCartItem']),
      // 点击商品跳到商品详情
      goToGoodDetail(id) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
        })
      },
      // 点击单选按钮
      handlerGoodsStatus(good) {
        // 取反后 将vuex数据修改
        this.updataGoodsStatus(good)
      },
      // 处理数量
      handlerGoodsNum(goods) {
        this.updateGoodsNum(goods)
      },
      swipeActionClickHandler(id) {
        this.deleteCartItem(id)
      }
    }
  }
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 50px;
  }
  .cart-logo {
    height: 40px;
    line-height: 40px;
    padding-left: 5px;
    font-size: 14px;
    border-bottom: 1px solid #efefef;

    text {
      margin-left: 10px;
    }
  }
  .settle-box {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 999;
  }
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 150px;
  
    .empty-img {
      width: 90px;
      height: 90px;
    }
  
    .tip-text {
      font-size: 12px;
      color: gray;
      margin-top: 15px;
    }
  }
</style>
